<?php
namespace app\manager\model;
use think\Validate;
/**
 * 菜单
 */
class Menus extends \think\Model
{
	protected $table = 'menus';
	protected $code = 'failed';
	protected $autoWriteTimestamp = 'timestamp';
	/**
	 * 保存菜单
	 */
	public function saveMenu($data){
		$re = $this->menuValidate($data);
		if($re['code'] == $this->code){return $re;}
	    try {
	        self::create($data, true);
	    } catch (\Exception $e) {
	        throw $e;
	    }
	}
	/**
	 * 编辑菜单
	 */
	public function updateMenu($data){
	    $re = $this->menuValidate($data);
		if($re['code'] == $this->code){return $re;}
	    try {
	    	unset($data['0']);
	        self::update($data, [], true);
	    } catch (\Exception $e) {
	        throw $e;
	    }
	}
	/**
	 * 验证
	 */
	public function menuValidate($data){
	    $validate = new Validate(
	    	[
		    	'name' 		=> 'require',
		    	'content'	=> 'require',
			],
			[
		        'name' => '菜单名称不能为空',
		        'content'   => '菜单内容不能为空',
		    ]
		);
		if(!$validate->check($data)){
			return ['code' => $this->code, 'msg' => $validate -> getError()];
		}
	}
	
}

?>